/**
 * 兼容低版本浏览器使用的方法
 * @param money 需要转换的金额（数字），最大 999999999999999.9999
 * @returns 转换后的中文大写
 */
function numberToChinese(money) {
  const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; //汉字的数字
  const cnIntRadice = ['', '拾', '佰', '仟']; //基本单位
  const cnIntUnits = ['', '万', '亿', '兆']; //对应整数部分扩展单位
  const cnDecUnits = ['角', '分', '毫', '厘']; //对应小数部分单位
  const cnInteger = '整'; //整数金额时后面跟的字符
  const cnIntLast = '元'; //整型完以后的单位
  const maxNum = 999999999999999.9999; //最大处理的数字
  let IntegerNum; //金额整数部分
  let DecimalNum; //金额小数部分
  let ChineseStr = ''; //输出的中文金额字符串
  let parts; //分离金额后用的数组，预定义
  let Symbol = ''; //正负值标记
  if (money == '') {
    return '';
  }

  money = parseFloat(money);
  if (money >= maxNum) {
    alert('超出最大处理数字');
    return '';
  }
  if (money == 0) {
    ChineseStr = cnNums[0] + cnIntLast + cnInteger;
    return ChineseStr;
  }
  if (money < 0) {
    money = -money;
    Symbol = '负 ';
  }
  money = money.toString(); //转换为字符串
  if (money.indexOf('.') == -1) {
    IntegerNum = money;
    DecimalNum = '';
  } else {
    parts = money.split('.');
    IntegerNum = parts[0];
    DecimalNum = parts[1].substr(0, 4);
  }
  if (parseInt(IntegerNum, 10) > 0) {
    //获取整型部分转换
    let zeroCount = 0;
    const IntLen = IntegerNum.length;
    for (let i = 0; i < IntLen; i++) {
      const n = IntegerNum.substr(i, 1);
      const p = IntLen - i - 1;
      const q = p / 4;
      const m = p % 4;
      if (n == '0') {
        zeroCount++;
      } else {
        if (zeroCount > 0) {
          ChineseStr += cnNums[0];
        }
        zeroCount = 0; //归零
        ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
      }
      if (m == 0 && zeroCount < 4) {
        ChineseStr += cnIntUnits[q];
      }
    }
    ChineseStr += cnIntLast;
    //整型部分处理完毕
  }
  if (DecimalNum != '') {
    //小数部分
    const decLen = DecimalNum.length;
    for (let i = 0; i < decLen; i++) {
      const n = DecimalNum.substr(i, 1);
      if (n != '0') {
        ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
      }
    }
  }
  if (ChineseStr == '') {
    ChineseStr += cnNums[0] + cnIntLast + cnInteger;
  } else if (DecimalNum == '') {
    ChineseStr += cnInteger;
  }
  ChineseStr = Symbol + ChineseStr;

  return ChineseStr;
}

function transformStr(money) {
  let cnMoney = '零元整';
  let strOutput = '';
  let strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分';
  money += '00';
  const intPos = money.indexOf('.');
  if (intPos >= 0) {
    money = money.substring(0, intPos) + money.substr(intPos + 1, 2);
  }
  strUnit = strUnit.substr(strUnit.length - money.length);
  for (let i = 0; i < money.length; i++) {
    strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(money.substr(i, 1), 1) + strUnit.substr(i, 1);
  }
  cnMoney = strOutput
    .replace(/零角零分$/, '整')
    .replace(/零[仟佰拾]/g, '零')
    .replace(/零{2,}/g, '零')
    .replace(/零([亿|万])/g, '$1')
    .replace(/零+元/, '元')
    .replace(/亿零{0,3}万/, '亿')
    .replace(/^元/, '零元');
  return cnMoney;
}

// or npm install pixiu-number-toolkit
// npm error code EUNSUPPORTEDPROTOCOL
// npm error Unsupported URL Type "link:": link:

export { numberToChinese, transformStr };
